<!DOCTYPE html><html lang="zh-CN" data-theme="light"><head><meta charset="UTF-8"><meta http-equiv="X-UA-Compatible" content="IE=edge"><meta name="viewport" content="width=device-width,initial-scale=1"><title>Java知识 | 小谭的部落阁</title><meta name="keywords" content="Java,JavaCore,Java基础"><meta name="author" content="谭自财"><meta name="copyright" content="谭自财"><meta name="format-detection" content="telephone=no"><meta name="theme-color" content="#ffffff"><meta name="description" content="JAVA基础语法注释、标识符、关键字  注释(Comments),在语言中并不会执行，给编写者和开发者阅读的文字，帮助编写者和开发者理解功能的意义，java有三种类型的注释,书写注释是一个非常好的习惯! 单行注释(Line comment)： &#x2F;&#x2F; 在java用两个斜线记做单行注释 &#x2F;&#x2F;这是一个java代码的单行注释，使用两条反斜杠   块注释&#x2F;多行注释(Block comment)： &#x2F;* *&#x2F;">
<meta property="og:type" content="article">
<meta property="og:title" content="Java知识">
<meta property="og:url" content="https://tanzicai.gitee.io/2022/02/09/JAVA%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/index.html">
<meta property="og:site_name" content="小谭的部落阁">
<meta property="og:description" content="JAVA基础语法注释、标识符、关键字  注释(Comments),在语言中并不会执行，给编写者和开发者阅读的文字，帮助编写者和开发者理解功能的意义，java有三种类型的注释,书写注释是一个非常好的习惯! 单行注释(Line comment)： &#x2F;&#x2F; 在java用两个斜线记做单行注释 &#x2F;&#x2F;这是一个java代码的单行注释，使用两条反斜杠   块注释&#x2F;多行注释(Block comment)： &#x2F;* *&#x2F;">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209165412.jpeg">
<meta property="article:published_time" content="2022-02-09T09:51:01.000Z">
<meta property="article:modified_time" content="2022-02-09T08:54:48.812Z">
<meta property="article:author" content="谭自财">
<meta property="article:tag" content="Java">
<meta property="article:tag" content="JavaCore">
<meta property="article:tag" content="Java基础">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209165412.jpeg"><link rel="shortcut icon" href="/img/favicon.png"><link rel="canonical" href="https://tanzicai.gitee.io/2022/02/09/JAVA%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/"><link rel="preconnect" href="//cdn.jsdelivr.net"/><link rel="preconnect" href="//busuanzi.ibruce.info"/><meta/><link rel="stylesheet" href="/css/index.css"><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free/css/all.min.css" media="print" onload="this.media='all'"><script>const GLOBAL_CONFIG = { 
  root: '/',
  algolia: undefined,
  localSearch: undefined,
  translate: {"defaultEncoding":2,"translateDelay":0,"msgToTraditionalChinese":"繁","msgToSimplifiedChinese":"简"},
  noticeOutdate: undefined,
  highlight: {"plugin":"highlighjs","highlightCopy":true,"highlightLang":true,"highlightHeightLimit":false},
  copy: {
    success: '复制成功',
    error: '复制错误',
    noSupport: '浏览器不支持'
  },
  relativeDate: {
    homepage: false,
    post: false
  },
  runtime: '天',
  date_suffix: {
    just: '刚刚',
    min: '分钟前',
    hour: '小时前',
    day: '天前',
    month: '个月前'
  },
  copyright: undefined,
  lightbox: 'fancybox',
  Snackbar: undefined,
  source: {
    jQuery: 'https://cdn.jsdelivr.net/npm/jquery@latest/dist/jquery.min.js',
    justifiedGallery: {
      js: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/js/jquery.justifiedGallery.min.js',
      css: 'https://cdn.jsdelivr.net/npm/justifiedGallery/dist/css/justifiedGallery.min.css'
    },
    fancybox: {
      js: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.js',
      css: 'https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@latest/dist/jquery.fancybox.min.css'
    }
  },
  isPhotoFigcaption: false,
  islazyload: false,
  isanchor: false
}</script><script id="config-diff">var GLOBAL_CONFIG_SITE = {
  title: 'Java知识',
  isPost: true,
  isHome: false,
  isHighlightShrink: false,
  isToc: true,
  postUpdate: '2022-02-09 16:54:48'
}</script><noscript><style type="text/css">
  #nav {
    opacity: 1
  }
  .justified-gallery img {
    opacity: 1
  }

  #recent-posts time,
  #post-meta time {
    display: inline !important
  }
</style></noscript><script>(win=>{
    win.saveToLocal = {
      set: function setWithExpiry(key, value, ttl) {
        if (ttl === 0) return
        const now = new Date()
        const expiryDay = ttl * 86400000
        const item = {
          value: value,
          expiry: now.getTime() + expiryDay,
        }
        localStorage.setItem(key, JSON.stringify(item))
      },

      get: function getWithExpiry(key) {
        const itemStr = localStorage.getItem(key)

        if (!itemStr) {
          return undefined
        }
        const item = JSON.parse(itemStr)
        const now = new Date()

        if (now.getTime() > item.expiry) {
          localStorage.removeItem(key)
          return undefined
        }
        return item.value
      }
    }
  
    win.getScript = url => new Promise((resolve, reject) => {
      const script = document.createElement('script')
      script.src = url
      script.async = true
      script.onerror = reject
      script.onload = script.onreadystatechange = function() {
        const loadState = this.readyState
        if (loadState && loadState !== 'loaded' && loadState !== 'complete') return
        script.onload = script.onreadystatechange = null
        resolve()
      }
      document.head.appendChild(script)
    })
  
      win.activateDarkMode = function () {
        document.documentElement.setAttribute('data-theme', 'dark')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#0d0d0d')
        }
      }
      win.activateLightMode = function () {
        document.documentElement.setAttribute('data-theme', 'light')
        if (document.querySelector('meta[name="theme-color"]') !== null) {
          document.querySelector('meta[name="theme-color"]').setAttribute('content', '#ffffff')
        }
      }
      const t = saveToLocal.get('theme')
    
          if (t === 'dark') activateDarkMode()
          else if (t === 'light') activateLightMode()
        
      const asideStatus = saveToLocal.get('aside-status')
      if (asideStatus !== undefined) {
        if (asideStatus === 'hide') {
          document.documentElement.classList.add('hide-aside')
        } else {
          document.documentElement.classList.remove('hide-aside')
        }
      }
    
    const fontSizeVal = saveToLocal.get('global-font-size')
    if (fontSizeVal !== undefined) {
      document.documentElement.style.setProperty('--global-font-size', fontSizeVal + 'px')
    }
    })(window)</script><meta name="generator" content="Hexo 5.4.0"><link rel="alternate" href="/atom.xml" title="小谭的部落阁" type="application/atom+xml">
</head><body><div id="loading-box"><div class="loading-left-bg"></div><div class="loading-right-bg"></div><div class="spinner-box"><div class="configure-border-1"><div class="configure-core"></div></div><div class="configure-border-2"><div class="configure-core"></div></div><div class="loading-word">加载中...</div></div></div><div id="sidebar"><div id="menu-mask"></div><div id="sidebar-menus"><div class="author-avatar"><img class="avatar-img" src="https://gitee.com/tanzicai/drawingbed/raw/master/img/20210609205810.png" onerror="onerror=null;src='/img/friend_404.gif'" alt="avatar"/></div><div class="site-data"><div class="data-item is-center"><div class="data-item-link"><a href="/archives/"><div class="headline">文章</div><div class="length-num">35</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div></div><div class="data-item is-center"><div class="data-item-link"><a href="/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div></div><hr/><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标籤</span></a></div><div class="menus_item"><a class="site-page" href="/category/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/fridends/"><i class="fa-fw fa fa-heartbeat"></i><span> 朋友圈</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/download/"><i class="fa-fw fas fa--cloud-download"></i><span> 下载</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-hear"></i><span> 关于</span></a></div></div></div></div><div class="post" id="body-wrap"><header class="post-bg" id="page-header" style="background-image: url('https://gitee.com/tanzicai/file-pan/raw/master/img/20220209165412.jpeg')"><nav id="nav"><span id="blog_name"><a id="site-name" href="/">小谭的部落阁</a></span><div id="menus"><div class="menus_items"><div class="menus_item"><a class="site-page" href="/"><i class="fa-fw fas fa-home"></i><span> 首页</span></a></div><div class="menus_item"><a class="site-page" href="/archives/"><i class="fa-fw fas fa-archive"></i><span> 时间轴</span></a></div><div class="menus_item"><a class="site-page" href="/tags/"><i class="fa-fw fas fa-tags"></i><span> 标籤</span></a></div><div class="menus_item"><a class="site-page" href="/category/"><i class="fa-fw fas fa-folder-open"></i><span> 分类</span></a></div><div class="menus_item"><a class="site-page" href="/fridends/"><i class="fa-fw fa fa-heartbeat"></i><span> 朋友圈</span></a></div><div class="menus_item"><a class="site-page" href="/link/"><i class="fa-fw fas fa-link"></i><span> 友链</span></a></div><div class="menus_item"><a class="site-page" href="/download/"><i class="fa-fw fas fa--cloud-download"></i><span> 下载</span></a></div><div class="menus_item"><a class="site-page" href="/about/"><i class="fa-fw fas fa-hear"></i><span> 关于</span></a></div></div><div id="toggle-menu"><a class="site-page"><i class="fas fa-bars fa-fw"></i></a></div></div></nav><div id="post-info"><h1 class="post-title">Java知识</h1><div id="post-meta"><div class="meta-firstline"><span class="post-meta-date"><i class="far fa-calendar-alt fa-fw post-meta-icon"></i><span class="post-meta-label">发表于</span><time class="post-meta-date-created" datetime="2022-02-09T09:51:01.000Z" title="发表于 2022-02-09 17:51:01">2022-02-09</time><span class="post-meta-separator">|</span><i class="fas fa-history fa-fw post-meta-icon"></i><span class="post-meta-label">更新于</span><time class="post-meta-date-updated" datetime="2022-02-09T08:54:48.812Z" title="更新于 2022-02-09 16:54:48">2022-02-09</time></span><span class="post-meta-categories"><span class="post-meta-separator">|</span><i class="fas fa-inbox fa-fw post-meta-icon"></i><a class="post-meta-categories" href="/categories/%E5%90%8E%E7%AB%AF/">后端</a></span></div><div class="meta-secondline"><span class="post-meta-separator">|</span><span class="post-meta-pv-cv" id="" data-flag-title="Java知识"><i class="far fa-eye fa-fw post-meta-icon"></i><span class="post-meta-label">阅读量:</span><span id="busuanzi_value_page_pv"></span></span></div></div></div></header><main class="layout" id="content-inner"><div id="post"><article class="post-content" id="article-container"><h1 id="JAVA基础语法"><a href="#JAVA基础语法" class="headerlink" title="JAVA基础语法"></a>JAVA基础语法</h1><h1 id="注释、标识符、关键字"><a href="#注释、标识符、关键字" class="headerlink" title="注释、标识符、关键字"></a>注释、标识符、关键字</h1><hr>
<ul>
<li>注释(Comments),在语言中并不会执行，给编写者和开发者阅读的文字，帮助编写者和开发者理解功能的意义，java有三种类型的注释,书写注释是一个非常好的习惯!<ul>
<li>单行注释(Line comment)： <code>//</code> 在java用两个斜线记做单行注释 <code>//这是一个java代码的单行注释，使用两条反斜杠</code>  </li>
<li>块注释/多行注释(Block comment)： /* */ 在java中用反斜杠加 *的方式来表示块注释 <code>/**/</code> 可以注释多行文本</li>
<li>文档注释(JavaDoc):<code>/** */</code>来表示JavaDoc，里面可以加描述<br>平时写代码一定要注意规范。</li>
</ul>
</li>
<li>关键字：一共53个关键字，Java所有的组成部分都需要名字。类名、变量名和方法名都被称为标识符。<br>  其中<code>51</code>个<code>关键字</code>，<code>2</code>个<code>保留字</code><br>  关键字+保留字：<br>  <strong>访问控制</strong>:private 私有的   protected 受保护的   public 公共的<strong>类、方法和变量修饰符</strong>abstract 声明抽象  class 类  extends 扩允,继承   final 终极,不可改变的implements实现   interface 接口   native 本地   new 新,创建    static 静态    strictfp 严格,精准synchronized 线程,同步    transient 短暂    volatile 易失 <strong>程序控制语句</strong> break 跳出循环    continue 继续   return 返回   do 运行   while 循环  if 如果  else 反之   for 循   instanceof 实例    switch 开关    case 返回开关里的结果   default 默认 <strong>错误处理</strong>  catch 处理异常      finally 有没有异常都执行     throw 抛出一个异常对象throws 声明一个异常可能被抛出      try 捕获异常<strong>包相关</strong>import 引入    package 包<strong>基本类型</strong>  boolean 布尔型   byte 字节型   char 字符型   double 双精度    float 浮点int 整型   long 长整型    short 短整型   null 空   true 真   false 假 <strong>变量引用</strong>  super 父类,超类   this 本类    void 无返回值 goto const<br>  Java 关键字列表 (依字母排序 共51组):  　　<br>  abstract, assert,boolean, break, byte, case, catch, char, class, const, continue, default, do, double, else, enum,extends, final, finally, float, for, if, implements, import, instanceof, int, interface, long, native, new, package, private, protected, public, return, short, static, strictfp, super, switch, synchronized, this, throw, throws, transient, try, void, volatile, while    Java 保留字列表 (依字母排序 共14组) :Java保留字是指现有Java版本尚未使用但以后版本可能会作为关键字使用。<br>  byValue, cast, false, future, generic, inner, operator, outer, rest, true, var ， goto ，const,null<br>  <img src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209165112.png"></li>
<li>标识符<br>  所有的标识符都以字符<code>(A-Z或者a-z)</code>，美元符(<code>$</code>)、或者下划线(<code>__</code>)开始，首字母之后可以是字母、美元符、下划线或者数字的任意字符组合；<br>  不能使用<code>关键字</code>作为<code>变量名</code>或者<code>方法名</code>，<em>标识符大小写敏感</em>；<br>  可以使用<code>中文命名</code>，但是一般不建议这样去使用，也不建议使用拼音，很Low；</li>
</ul>
<hr>
<h1 id="数据类型"><a href="#数据类型" class="headerlink" title="数据类型"></a>数据类型</h1><p>Java是一种强类型语言，要求变量的使用要严格符合规定，所有变量都必须先定义后使用；</p>
<h2 id="整数类型"><a href="#整数类型" class="headerlink" title="整数类型"></a>整数类型</h2><p><a target="_blank" rel="noopener" href="https://www.wolai.com/n3s8Dbi9zCxvSFhk6a5ujZ#uaf1YU1x6v9qGGYzDmGvSB">表格标题</a></p>
<h2 id="引用数据类型"><a href="#引用数据类型" class="headerlink" title="引用数据类型"></a>引用数据类型</h2><p><a target="_blank" rel="noopener" href="https://www.wolai.com/n3s8Dbi9zCxvSFhk6a5ujZ#tJ14BdGvX5f3RxcX12cykG">表格标题</a></p>
<h2 id="关于储存位数的说明"><a href="#关于储存位数的说明" class="headerlink" title="关于储存位数的说明"></a>关于储存位数的说明</h2><p><code>1</code>个<code>字节</code>(<code>byte</code>) = 8 位(<code>bit</code>)  ,例如 <code>11001010</code>，这是一个8<code>bit</code>， 每一个数字占据一个<code>bit</code></p>
<h1 id="类型转换"><a href="#类型转换" class="headerlink" title="类型转换"></a>类型转换</h1><p> <strong>在java中，整数拓展， 二进制 十进制 八进制 十六进制</strong></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">        </span><br><span class="line">低  ------------------------------------&gt;  高</span><br><span class="line"></span><br><span class="line"><span class="keyword">byte</span>,<span class="keyword">short</span>,<span class="keyword">char</span>—&gt; <span class="keyword">int</span> —&gt; <span class="keyword">long</span>—&gt; <span class="keyword">float</span> —&gt; <span class="keyword">double</span> </span><br><span class="line">    自动类型转换也叫“向上类型转换”，它是低级转为高级，例如 <span class="keyword">char</span> a=<span class="string">&#x27;a&#x27;</span>; <span class="keyword">int</span> a1=a; 这时就是向上类型转型，由<span class="keyword">char</span>类型转为<span class="keyword">int</span>类型a1=<span class="number">97</span>;</span><br><span class="line">    </span><br><span class="line">    <span class="keyword">int</span> i =<span class="number">10</span>;</span><br><span class="line">    <span class="keyword">int</span> i1 =<span class="number">010</span>; <span class="comment">//八进制 0开头</span></span><br><span class="line">    <span class="keyword">int</span> i2 =<span class="number">0x10</span>; <span class="comment">//十六进制 0x开头  0~9  A~F 16进制</span></span><br><span class="line">    <span class="keyword">int</span> i3 =<span class="number">0b11</span> <span class="comment">//二进制</span></span><br></pre></td></tr></table></figure>


<h2 id="浮点数拓展"><a href="#浮点数拓展" class="headerlink" title="浮点数拓展"></a><strong>浮点数拓展</strong></h2><p>浮点数 浮点数有限 离散 舍入误差， 接近但不等于</p>
<p>最好完全使用浮点数进行比较</p>
<p> 对于银行业务，我们尽量用类， <code>BigDecimal</code> 数学工具类</p>
<p> <strong>字符的拓展，我们可以通过强制类型转换，把字符转换为int， 所有的字符本质还是数字</strong></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo3</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> i =<span class="number">10</span>;</span><br><span class="line">        <span class="keyword">int</span> i1 =<span class="number">010</span>; <span class="comment">//八进制 0开头</span></span><br><span class="line">        <span class="keyword">int</span> i2 =<span class="number">0x10</span>; <span class="comment">//十六进制 0x开头  0~9  A~F 16进制</span></span><br><span class="line">        System.out.println(i);</span><br><span class="line">        System.out.println(i1);</span><br><span class="line">        System.out.println(i2);</span><br><span class="line">        System.out.println(<span class="string">&quot;====================&quot;</span>);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//浮点数拓展， 银行业务我们怎么表示钱？</span></span><br><span class="line">        <span class="keyword">float</span> f = <span class="number">0.1f</span>;  <span class="comment">//0.1</span></span><br><span class="line">        <span class="keyword">double</span> d = <span class="number">1.0</span>/<span class="number">10</span>; <span class="comment">//0.1</span></span><br><span class="line">        System.out.println(f==d); <span class="comment">//false</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">float</span> d1 = <span class="number">232323223232f</span>;</span><br><span class="line">        <span class="keyword">float</span> d2 = d1+<span class="number">1</span>;</span><br><span class="line">        System.out.println(d1==d2); <span class="comment">//返回了true</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br><span class="line"></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p>编码 Unicode 有个表，  每个数字对应一个字符，共2个字节，即共有<code>65533</code>个字符</p>
<p>$$<br>2^{16} =65533<br>$$</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">char</span> c1 =<span class="string">&#x27;a&#x27;</span>;</span><br><span class="line"><span class="keyword">char</span> c2 = <span class="string">&#x27;中&#x27;</span>;</span><br><span class="line"></span><br><span class="line"><span class="keyword">char</span> c3 = <span class="string">&#x27;\u0061&#x27;</span>;</span><br><span class="line"></span><br><span class="line">System.out.println(c1);</span><br><span class="line">System.out.println((<span class="keyword">int</span>)c1);</span><br><span class="line"></span><br><span class="line">System.out.println(c2);</span><br><span class="line">System.out.println((<span class="keyword">int</span>)c2);</span><br></pre></td></tr></table></figure>


<h2 id="转义字符"><a href="#转义字符" class="headerlink" title="转义字符"></a>转义字符</h2><p><code>\t</code>  一个制表符  </p>
<p><code>\n</code> 一个换行  </p>
<p><code>\b</code> 一个空格</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">String sa = <span class="keyword">new</span> String(<span class="string">&quot;Hello World!&quot;</span>);</span><br><span class="line">String sb = <span class="keyword">new</span> String(<span class="string">&quot;Hello World!&quot;</span>);</span><br><span class="line">System.out.println(sa==sb); <span class="comment">//false</span></span><br><span class="line"></span><br><span class="line">String sc = <span class="string">&quot;Hello World!&quot;</span>;</span><br><span class="line">String sd = <span class="string">&quot;Hello World!&quot;</span>;</span><br><span class="line">System.out.println(sc==sd);  <span class="comment">// true</span></span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h2 id="布尔值扩展"><a href="#布尔值扩展" class="headerlink" title="布尔值扩展"></a><strong>布尔值扩展</strong></h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">System.out.println(<span class="string">&quot;===========================&quot;</span>);</span><br><span class="line"><span class="keyword">boolean</span> flag = <span class="keyword">true</span>;</span><br><span class="line"><span class="keyword">if</span> (flag==<span class="keyword">true</span>)&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;这是真&quot;</span>);</span><br><span class="line">&#125;</span><br><span class="line"><span class="keyword">if</span> (flag)&#123;</span><br><span class="line">    System.out.println(<span class="string">&quot;这是真&quot;</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="类型转换-amp-amp-强制转换"><a href="#类型转换-amp-amp-强制转换" class="headerlink" title="类型转换&amp;&amp;强制转换"></a><strong>类型转换&amp;&amp;强制转换</strong></h2><p>低 —————————————————–&gt;高</p>
<p>byte, short ,char —&gt;int –&gt;long—&gt;float—&gt;double //小数的优先级高于整数</p>
<p>运算中，不同类型的数据先要转化为同一个类型，然后再进行运算，不同类型之间的运算系统会自动转换</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> a = <span class="number">128</span>;</span><br><span class="line"><span class="keyword">byte</span> demo = (<span class="keyword">byte</span>)a;  <span class="comment">//强制高位转换低位，128超出了byte的表达范围，导致内存溢出 </span></span><br><span class="line">System.out.println(a);</span><br><span class="line">System.out.println(demo);</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<p>强制类型转换也叫“向下类型转型”，它是高级转为低级，例如</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> i1 = <span class="number">123</span>;        </span><br><span class="line"><span class="keyword">byte</span> b = (<span class="keyword">byte</span>)i1;<span class="comment">//强制类型转换为byte</span></span><br></pre></td></tr></table></figure>


<p>在Java中强制类型转换分为<code>基本数据类型</code>和<code>引用数据类型</code>两种，这里我们讨论的后者，也就是引用数据类型的强制类型转换。</p>
<p>在Java中由于<code>继承</code>和<code>向上转型</code> ,<code>子类</code>可以非常自然地转换成<code>父类</code>，但是<code>父类</code>转换成<code>子类</code>则需要强制转换。因为子类拥有比父类更多的属性、更强的功能，所以父类转换为子类需要强制。</p>
<p>那么，是不是只要是父类转换为子类就会成功呢？其实不然，他们之间的强制类型转换是有条件的。</p>
<p>当我们用一个类型的构造器构造出一个对象时，这个对象的类型就已经确定的，也就说它的本质是不会再发生变化了。在Java中我们可以通过<code>继承</code>、<code>向上转型</code>的关系使用父类类型来引用它，这个时候我们是使用功能较弱的类型引用功能较强的对象，这是可行的。但是将功能较弱的类型强制转功能较强的对象时，就<code>不一定</code>可以行了。</p>
<p>举个例子来说明。比如系统中存在Father、Son两个对象。首先我们先构造一个Son对象，然后用一个Father类型变量引用它：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Father father = <span class="keyword">new</span> Son();</span><br></pre></td></tr></table></figure>


<p>在这里Son 对象实例被<code>向上转型</code>为father了，但是请注意这个<code>Son对象实例</code>在<code>内存中</code>的<code>本质</code>还是<code>Son类型</code>的，只不过它的能力<code>临时</code>被消弱了而已，如果我们想变强怎么办？将其对象类型还原！</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Son son = (Son)father;</span><br></pre></td></tr></table></figure>


<p>这条语句是可行的，其实<code>father引用</code>仍然是<code>Father类型</code>的，只不过是将它的能力加强了，将其加强后转交给<code>son引用</code>了，<code>Son对象实例</code>在<code>son的变量</code>的引用下，<code>恢复真身</code>，可以使用全部功能了。</p>
<p>前面提到<code>父类</code>强制转换成<code>子类</code>并不是总是成功，那么在什么情况下它会失效呢？</p>
<p>当引用类型的真实身份是父类本身的类型时，强制类型转换就会产生错误。例如：</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">Father father = <span class="keyword">new</span> Father();</span><br><span class="line">Son son = (Son) father;</span><br></pre></td></tr></table></figure>


<p>这个系统会抛出ClassCastException异常信息。</p>
<p>所以编译器在<code>编译时</code>只会<code>检查类型之间</code>是否存在<code>继承关系</code>，有则通过；</p>
<p>而在<code>运行时</code>就会检查它的<code>真实类型</code>，是则通过，否则抛出<code>ClassCastException</code>异常。</p>
<p>所以在继承中，子类可以自动转型为父类，但是父类强制转换为子类时只有当<code>引用类型真正的身份</code>为<code>子类</code>时才会强制转换成功，否则失败</p>
<h2 id="注意点"><a href="#注意点" class="headerlink" title="注意点"></a>注意点</h2><ol>
<li><code>**不能对布尔值进行转换;**</code></li>
<li><code>**不能把对象类型转换为不相干的类型**</code></li>
<li><code>**大容量转换为低容量，需要强制转换，低容量转换高容量，不需要强制；**</code></li>
<li><code>**转换时候可能内存溢出或者精度的问题**</code></li>
</ol>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">System.out.println((<span class="keyword">int</span>)<span class="number">23.7</span>);  <span class="comment">//23</span></span><br><span class="line">System.out.println((<span class="keyword">int</span>)-<span class="number">45.89f</span>);  <span class="comment">//-45</span></span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">int</span> money = <span class="number">10_0000_0000</span>;</span><br><span class="line"><span class="keyword">int</span> years = <span class="number">20</span>;</span><br><span class="line"><span class="keyword">int</span> total = money*years; <span class="comment">//-1474992996480  计算的时候溢出了</span></span><br><span class="line"><span class="keyword">long</span> total2 = money*years; <span class="comment">//-147399996480 ,默认是int，转换之前已经存在了问题</span></span><br><span class="line"><span class="keyword">long</span> total3 =money*((<span class="keyword">long</span>)years); <span class="comment">//20000000000，正确，需要先把一个数转换为long</span></span><br></pre></td></tr></table></figure>


<h1 id="变量、常量、作用域"><a href="#变量、常量、作用域" class="headerlink" title="变量、常量、作用域"></a>变量、常量、作用域</h1><p>变量是什么？ 就是可以变化的量，java是一种强类型语言，每个变量都必须声明其类型，java变量是程序中最基本的存储单元，其要素包括变量名，变量类型和作用域。</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line">type varName [=value] [&#123;, varname=[value]&#125;];</span><br><span class="line"><span class="comment">//数据类型  变量名 =值；可以用逗号隔开来声明多个同类型的变量。  </span></span><br></pre></td></tr></table></figure>


<p>注意事项：</p>
<ol>
<li>每个变量都有类型，类型可以是<code>基本类型</code>，也可以是<code>衍生类型</code></li>
<li>变量名必须是<code>合法的标识符</code></li>
<li>变量声明是一条完整的语句，因此没一个声明都必须以分号结束</li>
</ol>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="comment">//int a,b,c;</span></span><br><span class="line"><span class="comment">// int a=1,b=2,c=3;  //这种方式程序可读性比较差，在实际代码中，不建议这种方式</span></span><br><span class="line">String name = <span class="string">&quot;qinjiang&quot;</span>;</span><br><span class="line"><span class="keyword">char</span> x = <span class="string">&#x27;X&#x27;</span>;</span><br><span class="line"><span class="keyword">double</span> pi =<span class="number">3.14</span>;</span><br><span class="line"></span><br></pre></td></tr></table></figure>


<h2 id="作用域"><a href="#作用域" class="headerlink" title="作用域"></a>作用域</h2><p>在java有三种作用域，分别是 <code>类变量</code>，<code>实例变量</code>，<code>局部变量</code></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo8</span> </span>&#123;</span><br><span class="line">    <span class="comment">//属性:变量</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">//类变量 static修饰的变量为类变量</span></span><br><span class="line">    <span class="keyword">static</span> <span class="keyword">double</span> salary = <span class="number">2500</span>; <span class="comment">//可以直接输出，直接使用</span></span><br><span class="line">  </span><br><span class="line">    <span class="comment">//实例变量: 从属于对象，通过这个类才能使用,如果没有初始化，会使用它的默认值, 布尔值默认是false ;</span></span><br><span class="line">    <span class="comment">// 除了基本类型，其他的默认值都是null;</span></span><br><span class="line">    String name ;</span><br><span class="line">    <span class="keyword">int</span> age;</span><br><span class="line"></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//局部变量，必须声明和初始化</span></span><br><span class="line">        <span class="keyword">int</span> i = <span class="number">10</span>;</span><br><span class="line">        System.out.println(i);</span><br><span class="line"></span><br><span class="line">        <span class="comment">//变量类型 变量名字= new Demo8()</span></span><br><span class="line">        Demo8 demo8 = <span class="keyword">new</span> Demo8();</span><br><span class="line">        demo8.age = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">    <span class="comment">//其他方法</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">void</span> <span class="title">add</span><span class="params">()</span></span>&#123;</span><br><span class="line">        System.out.println(i); <span class="comment">//i是一个局部变量，这个i就在main定义，无法在add使用</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>常量可以理解为一个特殊变量，一旦定义不可改变, 一般常量都用大写字符表示，提高可读性</p>
<p><code>final 常量名=常量值</code></p>
<p><code>static final double PI = 3.14</code></p>
<p>修饰符不存在先后顺序</p>
<p><code>final static double PI=3.14</code></p>
<h2 id="变量命名规范"><a href="#变量命名规范" class="headerlink" title="变量命名规范"></a>变量命名规范</h2><ol>
<li>所有变量、方法、类名，见名知意</li>
<li>类成员变量，首字符小写和驼峰原则，:<code> monthSalary</code>，除了第一个单词意外，后面的单词首字母大写</li>
<li>局部变量， 首字符小写和驼峰原则,</li>
<li>常量，<code>大写字母和下划线</code> :<code> MAX_LENGTH</code>,<code> MAX_VALUE</code></li>
<li>类名，首字符大写和驼峰原则 , <code>Man</code>, <code>GoodMan</code></li>
<li>方法名， 首字母小写和驼峰原则 :<code>run()</code>, <code>runRun()</code></li>
</ol>
<h1 id="基本的运算符"><a href="#基本的运算符" class="headerlink" title="基本的运算符"></a>基本的运算符</h1><p>算术运算符 <code>+ - * / % ++ --</code></p>
<p>赋值运算符 <code>=</code></p>
<p>关系运算符 <code>&gt;</code> ，<code>&lt;</code>， <code>&gt;=</code>， <code>&lt;=</code> ，<code>==</code> ， <code>!=</code> ， <code>instanceof</code></p>
<p>逻辑运算符: <code>&amp;&amp;</code>与， <code>||</code> 或， <code>!</code>非</p>
<p>位运算符 <code>&amp;</code> ， <code>|</code>， <code>^</code> <code>~</code> <code>&gt;&gt;</code> <code>&lt;&lt;</code> <code>&gt;&gt;&gt;</code></p>
<p>条件运算法 <code>?:</code></p>
<p>扩展赋值运算符 <code>+=</code> <code>-=</code> <code>*=</code> <code>/=</code></p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">    <span class="comment">//二元运算符</span></span><br><span class="line">    <span class="comment">//Ctrl+D 复制当前行到下一行</span></span><br><span class="line">    <span class="keyword">int</span> a = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">int</span> b =<span class="number">20</span>;</span><br><span class="line">    <span class="keyword">int</span> c = <span class="number">10</span>;</span><br><span class="line">    <span class="keyword">int</span> d = <span class="number">10</span>;</span><br><span class="line"></span><br><span class="line">    System.out.println(a+b);</span><br><span class="line">    System.out.println(a-b);</span><br><span class="line">    System.out.println(a*b);</span><br><span class="line">    System.out.println(a/(<span class="keyword">double</span>)b);</span><br><span class="line"></span><br><span class="line">    <span class="keyword">long</span> e = <span class="number">12312312313123L</span>;</span><br><span class="line">    <span class="keyword">int</span> j = <span class="number">123</span>;</span><br><span class="line">    <span class="keyword">short</span> f =<span class="number">10</span>;</span><br><span class="line">    <span class="keyword">byte</span> h =<span class="number">8</span>;</span><br><span class="line"></span><br><span class="line">    System.out.println(e+f+j+h); </span><br><span class="line">    <span class="comment">// 输出Long,如果运算中有Long类型，运算结果一定是Long</span></span><br><span class="line">    System.out.println(f+j+h); </span><br><span class="line">    <span class="comment">//输出int类型 如果运算中有int类型，运算结果一定是int类型</span></span><br><span class="line">    System.out.println(f+h); </span><br><span class="line">    <span class="comment">//输出int类型</span></span><br><span class="line">  &#125;</span><br></pre></td></tr></table></figure>


<p><strong>类型向上转换，在同一个运算关系中，含有不同类型，则向上进行类型转换</strong></p>
<p><code>++</code>  <code>--</code>的运算时机，根据符号所在的变量位置，从而的出值    </p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> operator;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo4</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//++ -- 自加，自减 一元运算符</span></span><br><span class="line">        <span class="keyword">int</span> a =<span class="number">3</span>;</span><br><span class="line">        <span class="keyword">int</span> b =a++;</span><br><span class="line">        <span class="comment">// a=a+1  先使用，后自加</span></span><br><span class="line">        </span><br><span class="line">        <span class="comment">//a=a+1 先自加，后使用</span></span><br><span class="line">        <span class="keyword">int</span> c = ++a;</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p><code>a++</code> 先使用，后自加  <code>++a</code>，先自加，后使用；</p>
<p>所有自加，自减，原来是的变量值都会发生变化</p>
<h2 id="逻辑运算符-amp-amp-与-或-非"><a href="#逻辑运算符-amp-amp-与-或-非" class="headerlink" title="逻辑运算符  &amp;&amp;与  ||或  !非"></a>逻辑运算符  &amp;&amp;与  ||或  !非</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> operator;</span><br><span class="line"></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo5</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">boolean</span> a = <span class="keyword">true</span>;</span><br><span class="line">        <span class="keyword">boolean</span> b =<span class="keyword">false</span>;</span><br><span class="line"></span><br><span class="line">        System.out.println(<span class="string">&quot;a &amp;&amp; b:&quot;</span>+(a&amp;&amp;b));</span><br><span class="line">        <span class="comment">// 逻辑与运算，两个变量都为真，结果才为true</span></span><br><span class="line">        System.out.println(<span class="string">&quot;a || b:&quot;</span>+(a||b)); </span><br><span class="line">        <span class="comment">//逻辑或运算，两个变量中有一个为真，则结果为true</span></span><br><span class="line">        System.out.println(<span class="string">&quot;!(a &amp;&amp; b:)&quot;</span>+!(a&amp;&amp;b)); </span><br><span class="line">        <span class="comment">//如果是真，则变为假，如果是假则为真</span></span><br><span class="line"></span><br><span class="line">        <span class="comment">//短路运算</span></span><br><span class="line">        <span class="keyword">int</span> c=<span class="number">5</span>;</span><br><span class="line">        <span class="keyword">boolean</span> d = (c&lt;<span class="number">4</span>)&amp;&amp;(c++&lt;<span class="number">4</span>);</span><br><span class="line">        System.out.println(d);</span><br><span class="line">        System.out.println(c); </span><br><span class="line">        <span class="comment">//上述运算执行结束，并没有运算c++&lt;4</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>字符串连接符， +, 左右两侧只有有一个出现String，则最终结果就会出现String</p>
<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">demo6</span> </span>&#123;</span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="keyword">int</span> a =<span class="number">10</span>;</span><br><span class="line">        <span class="keyword">int</span> b =<span class="number">20</span>;</span><br><span class="line">        a+=b;</span><br><span class="line">        a-=b;</span><br><span class="line">        System.out.println(a); <span class="comment">//输出10</span></span><br><span class="line">        <span class="comment">//字符串连接符  +</span></span><br><span class="line">        System.out.println(<span class="string">&quot;&quot;</span>+a+b); <span class="comment">//输出1020</span></span><br><span class="line">        System.out.println(a+b+<span class="string">&quot;-&quot;</span>);  <span class="comment">//输出30-</span></span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h2 id="三元运算符"><a href="#三元运算符" class="headerlink" title="三元运算符"></a>三元运算符</h2><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">x ? y : z , 如果x为真，则是Y， 否则就是Z</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Demo7</span> </span>&#123;</span><br><span class="line">    <span class="comment">//三元运算符</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> <span class="keyword">static</span> <span class="keyword">void</span> <span class="title">main</span><span class="params">(String[] args)</span> </span>&#123;</span><br><span class="line">        <span class="comment">//x ? y : z 如果x==true， 则结果为y， 否则结果为z</span></span><br><span class="line"></span><br><span class="line">        <span class="keyword">int</span> sore = <span class="number">80</span>;</span><br><span class="line">        String  type = (sore&lt;<span class="number">60</span>) ? <span class="string">&quot;不及格&quot;</span>:<span class="string">&quot;及格&quot;</span>; <span class="comment">//必须掌握这个用法</span></span><br><span class="line">        System.out.println(type);</span><br><span class="line">    &#125;</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<h1 id="包机制"><a href="#包机制" class="headerlink" title="包机制"></a>包机制</h1><p>为了刚好的组织类，java提供了包机制，提供命名空间， 包的本质就是一个文件夹，防止命名冲突包语言的规范，</p>
<ul>
<li>一般用公司域名导致作为包名,防止包名重复: <a href="www.baidu.com">www.baidu.com</a> <code>com\baidu\www</code> <code>com\baidu\baike</code> <code>com\baidu\zhidao</code></li>
<li>包通过<code>package</code>开头，必须在文件开头， <code>package com.baidu.www;</code></li>
<li>我们需要使用其他包的内容，就需要使用<code>import java.util.Date;</code></li>
<li>推荐看一个内容，阿里巴巴开发手册。</li>
</ul>
<h1 id="Java-Doc"><a href="#Java-Doc" class="headerlink" title="Java Doc"></a>Java Doc</h1><figure class="highlight java"><table><tr><td class="code"><pre><span class="line">参数信息 </span><br><span class="line"></span><br><span class="line"><span class="meta">@author</span> 作者名</span><br><span class="line"></span><br><span class="line"><span class="meta">@version</span> 版本</span><br><span class="line"></span><br><span class="line"><span class="meta">@since</span> 指明需要最早使用的jdk版本</span><br><span class="line"></span><br><span class="line"><span class="meta">@param</span> 参数名</span><br><span class="line"></span><br><span class="line"><span class="meta">@return</span>  返回值情况</span><br><span class="line"></span><br><span class="line"><span class="meta">@throws</span> 异常抛出情况</span><br></pre></td></tr></table></figure>


<figure class="highlight java"><table><tr><td class="code"><pre><span class="line"><span class="keyword">package</span> operator;</span><br><span class="line"></span><br><span class="line"><span class="comment">/**</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@author</span> luojg</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@version</span> 1.0</span></span><br><span class="line"><span class="comment"> * <span class="doctag">@since</span> 1.8</span></span><br><span class="line"><span class="comment"> * 这是一个类的文档解释</span></span><br><span class="line"><span class="comment"> */</span></span><br><span class="line"><span class="keyword">public</span> <span class="class"><span class="keyword">class</span> <span class="title">Doc</span> </span>&#123;</span><br><span class="line">    String name; <span class="comment">//这是一个类变量</span></span><br><span class="line"></span><br><span class="line">    <span class="comment">/**</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@author</span> luojg</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@param</span> 参数</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@return</span> 返回的值</span></span><br><span class="line"><span class="comment">     * <span class="doctag">@throws</span> Exception</span></span><br><span class="line"><span class="comment">     */</span></span><br><span class="line">    <span class="function"><span class="keyword">public</span> String <span class="title">test</span><span class="params">(String name)</span> <span class="keyword">throws</span> Exception </span>&#123;</span><br><span class="line">        <span class="keyword">return</span> name;</span><br><span class="line">    &#125;</span><br><span class="line"></span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>


<p>生成Java Doc的指令为</p>
<figure class="highlight bash"><table><tr><td class="code"><pre><span class="line">javadoc -encoding UTF-8 -charset UTF-8 Doc.java</span><br></pre></td></tr></table></figure>



</article><div class="post-copyright"><div class="post-copyright__author"><span class="post-copyright-meta">文章作者: </span><span class="post-copyright-info"><a href="mailto:undefined">谭自财</a></span></div><div class="post-copyright__type"><span class="post-copyright-meta">文章链接: </span><span class="post-copyright-info"><a href="https://tanzicai.gitee.io/2022/02/09/JAVA%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95/">https://tanzicai.gitee.io/2022/02/09/JAVA基础语法/</a></span></div><div class="post-copyright__notice"><span class="post-copyright-meta">版权声明: </span><span class="post-copyright-info">本博客所有文章除特别声明外，均采用 <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/" target="_blank">CC BY-NC-SA 4.0</a> 许可协议。转载请注明来自 <a href="https://tanzicai.gitee.io" target="_blank">小谭的部落阁</a>！</span></div></div><div class="tag_share"><div class="post-meta__tag-list"><a class="post-meta__tags" href="/tags/Java/">Java</a><a class="post-meta__tags" href="/tags/JavaCore/">JavaCore</a><a class="post-meta__tags" href="/tags/Java%E5%9F%BA%E7%A1%80/">Java基础</a></div><div class="post_share"><div class="social-share" data-image="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209165412.jpeg" data-sites="facebook,twitter,wechat,weibo,qq"></div><link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/social-share.js/dist/css/share.min.css" media="print" onload="this.media='all'"><script src="https://cdn.jsdelivr.net/npm/social-share.js/dist/js/social-share.min.js" defer></script></div></div><nav class="pagination-post" id="pagination"><div class="prev-post pull-left"><a href="/2022/02/09/Java%E6%B5%81%E7%A8%8B/"><img class="prev-cover" src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209170711.com&amp;app=2002&amp;size=f9999,10000&amp;q=a80&amp;n=0&amp;g=0n&amp;fmt=jpeg" onerror="onerror=null;src='/img/404.jpg'" alt="cover of previous post"><div class="pagination-info"><div class="label">上一篇</div><div class="prev_info">Java流程</div></div></a></div><div class="next-post pull-right"><a href="/2022/01/17/%E5%81%87%E6%9C%9F%E8%AE%A1%E5%88%92-%E5%B0%8F%E7%A8%8B%E5%BA%8F%E5%BC%80%E5%8F%91%E7%9B%AE%E5%BD%95%E7%BB%93%E6%9E%84/"><img class="next-cover" src="/img/survivors_v02_5120x2880.png" onerror="onerror=null;src='/img/404.jpg'" alt="cover of next post"><div class="pagination-info"><div class="label">下一篇</div><div class="next_info">假期计划-小程序开发目录结构</div></div></a></div></nav><div class="relatedPosts"><div class="headline"><i class="fas fa-thumbs-up fa-fw"></i><span> 相关推荐</span></div><div class="relatedPosts-list"><div><a href="/2021/03/21/JavaCore实践-时钟显示/" title="JavaCore实践-时钟显示"><img class="cover" src="/img/survivors_v02_5120x2880.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-21</div><div class="title">JavaCore实践-时钟显示</div></div></a></div><div><a href="/2021/03/21/Java学习路线/" title="Java学习路线"><img class="cover" src="/img/CTTN.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2021-03-21</div><div class="title">Java学习路线</div></div></a></div><div><a href="/2022/02/10/常用类/" title="Java常用类"><img class="cover" src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209171157.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-10</div><div class="title">Java常用类</div></div></a></div><div><a href="/2022/02/09/异常/" title="Java异常"><img class="cover" src="/img/survivors_v03_5120x2880.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java异常</div></div></a></div><div><a href="/2022/02/09/面向对象编程/" title="Java面向对象编程"><img class="cover" src="/img/CTTN.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java面向对象编程</div></div></a></div><div><a href="/2022/02/09/数组详解/" title="Java数组详解"><img class="cover" src="/img/CNYN.png" alt="cover"><div class="content is-center"><div class="date"><i class="far fa-calendar-alt fa-fw"></i> 2022-02-09</div><div class="title">Java数组详解</div></div></a></div></div></div></div><div class="aside-content" id="aside-content"><div class="card-widget card-info"><div class="card-info-avatar is-center"><img class="avatar-img" src="https://gitee.com/tanzicai/drawingbed/raw/master/img/20210609205810.png" onerror="this.onerror=null;this.src='/img/friend_404.gif'" alt="avatar"/><div class="author-info__name">谭自财</div><div class="author-info__description">你别卷入世俗，我别沉迷初遇</div></div><div class="card-info-data"><div class="card-info-data-item is-center"><a href="/archives/"><div class="headline">文章</div><div class="length-num">35</div></a></div><div class="card-info-data-item is-center"><a href="/tags/"><div class="headline">标签</div><div class="length-num">50</div></a></div><div class="card-info-data-item is-center"><a href="/categories/"><div class="headline">分类</div><div class="length-num">12</div></a></div></div><a class="button--animated" id="card-info-btn" target="_blank" rel="noopener" href="https://github.com/tanzicai"><i class="fab fa-github"></i><span>Follow Me</span></a><div class="card-info-social-icons is-center"><a class="social-icon" href="https://github.com/tanzicai" target="_blank" title="Github"><i class="fab fa-github"></i></a><a class="social-icon" href="https://gitee.com/tanzicai" target="_blank" title="Github"><i class="fab fa-git-square"></i></a><a class="social-icon" href="http://wpa.qq.com/msgrd?v=3&amp;uin=1728561744&amp;site=qq&amp;menu=yes" target="_blank" title="QQ"><i class="fas fa-qq"></i></a><a class="social-icon" href="mailto:1728561744@qq.com" target="_blank" title="Email"><i class="fas fa-envelope"></i></a></div></div><div class="card-widget card-announcement"><div class="item-headline"><i class="fas fa-bullhorn card-announcement-animation"></i><span>公告</span></div><div class="announcement_content">本站正在切换主题测试，如有错误提示，请联系我，谢谢</div></div><div class="sticky_layout"><div class="card-widget" id="card-toc"><div class="item-headline"><i class="fas fa-stream"></i><span>目录</span></div><div class="toc-content"><ol class="toc"><li class="toc-item toc-level-1"><a class="toc-link" href="#JAVA%E5%9F%BA%E7%A1%80%E8%AF%AD%E6%B3%95"><span class="toc-number">1.</span> <span class="toc-text">JAVA基础语法</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%B3%A8%E9%87%8A%E3%80%81%E6%A0%87%E8%AF%86%E7%AC%A6%E3%80%81%E5%85%B3%E9%94%AE%E5%AD%97"><span class="toc-number">2.</span> <span class="toc-text">注释、标识符、关键字</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">3.</span> <span class="toc-text">数据类型</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%95%B4%E6%95%B0%E7%B1%BB%E5%9E%8B"><span class="toc-number">3.1.</span> <span class="toc-text">整数类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%BC%95%E7%94%A8%E6%95%B0%E6%8D%AE%E7%B1%BB%E5%9E%8B"><span class="toc-number">3.2.</span> <span class="toc-text">引用数据类型</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%85%B3%E4%BA%8E%E5%82%A8%E5%AD%98%E4%BD%8D%E6%95%B0%E7%9A%84%E8%AF%B4%E6%98%8E"><span class="toc-number">3.3.</span> <span class="toc-text">关于储存位数的说明</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2"><span class="toc-number">4.</span> <span class="toc-text">类型转换</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B5%AE%E7%82%B9%E6%95%B0%E6%8B%93%E5%B1%95"><span class="toc-number">4.1.</span> <span class="toc-text">浮点数拓展</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E8%BD%AC%E4%B9%89%E5%AD%97%E7%AC%A6"><span class="toc-number">4.2.</span> <span class="toc-text">转义字符</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%B8%83%E5%B0%94%E5%80%BC%E6%89%A9%E5%B1%95"><span class="toc-number">4.3.</span> <span class="toc-text">布尔值扩展</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E7%B1%BB%E5%9E%8B%E8%BD%AC%E6%8D%A2-amp-amp-%E5%BC%BA%E5%88%B6%E8%BD%AC%E6%8D%A2"><span class="toc-number">4.4.</span> <span class="toc-text">类型转换&amp;&amp;强制转换</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E6%B3%A8%E6%84%8F%E7%82%B9"><span class="toc-number">4.5.</span> <span class="toc-text">注意点</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E3%80%81%E5%B8%B8%E9%87%8F%E3%80%81%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">5.</span> <span class="toc-text">变量、常量、作用域</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%BD%9C%E7%94%A8%E5%9F%9F"><span class="toc-number">5.1.</span> <span class="toc-text">作用域</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E5%8F%98%E9%87%8F%E5%91%BD%E5%90%8D%E8%A7%84%E8%8C%83"><span class="toc-number">5.2.</span> <span class="toc-text">变量命名规范</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%9F%BA%E6%9C%AC%E7%9A%84%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">6.</span> <span class="toc-text">基本的运算符</span></a><ol class="toc-child"><li class="toc-item toc-level-2"><a class="toc-link" href="#%E9%80%BB%E8%BE%91%E8%BF%90%E7%AE%97%E7%AC%A6-amp-amp-%E4%B8%8E-%E6%88%96-%E9%9D%9E"><span class="toc-number">6.1.</span> <span class="toc-text">逻辑运算符  &amp;&amp;与  ||或  !非</span></a></li><li class="toc-item toc-level-2"><a class="toc-link" href="#%E4%B8%89%E5%85%83%E8%BF%90%E7%AE%97%E7%AC%A6"><span class="toc-number">6.2.</span> <span class="toc-text">三元运算符</span></a></li></ol></li><li class="toc-item toc-level-1"><a class="toc-link" href="#%E5%8C%85%E6%9C%BA%E5%88%B6"><span class="toc-number">7.</span> <span class="toc-text">包机制</span></a></li><li class="toc-item toc-level-1"><a class="toc-link" href="#Java-Doc"><span class="toc-number">8.</span> <span class="toc-text">Java Doc</span></a></li></ol></div></div><div class="card-widget card-recent-post"><div class="item-headline"><i class="fas fa-history"></i><span>最新文章</span></div><div class="aside-list"><div class="aside-list-item"><a class="thumbnail" href="/2022/02/10/%E5%B8%B8%E7%94%A8%E7%B1%BB/" title="Java常用类"><img src="https://gitee.com/tanzicai/file-pan/raw/master/img/20220209171157.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java常用类"/></a><div class="content"><a class="title" href="/2022/02/10/%E5%B8%B8%E7%94%A8%E7%B1%BB/" title="Java常用类">Java常用类</a><time datetime="2022-02-10T12:25:00.000Z" title="发表于 2022-02-10 20:25:00">2022-02-10</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E5%BC%82%E5%B8%B8/" title="Java异常"><img src="/img/survivors_v03_5120x2880.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java异常"/></a><div class="content"><a class="title" href="/2022/02/09/%E5%BC%82%E5%B8%B8/" title="Java异常">Java异常</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" title="Java面向对象编程"><img src="/img/CTTN.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java面向对象编程"/></a><div class="content"><a class="title" href="/2022/02/09/%E9%9D%A2%E5%90%91%E5%AF%B9%E8%B1%A1%E7%BC%96%E7%A8%8B/" title="Java面向对象编程">Java面向对象编程</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E6%95%B0%E7%BB%84%E8%AF%A6%E8%A7%A3/" title="Java数组详解"><img src="/img/CNYN.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java数组详解"/></a><div class="content"><a class="title" href="/2022/02/09/%E6%95%B0%E7%BB%84%E8%AF%A6%E8%A7%A3/" title="Java数组详解">Java数组详解</a><time datetime="2022-02-09T13:25:00.000Z" title="发表于 2022-02-09 21:25:00">2022-02-09</time></div></div><div class="aside-list-item"><a class="thumbnail" href="/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/" title="Java方法详解"><img src="/img/WHT.png" onerror="this.onerror=null;this.src='/img/404.jpg'" alt="Java方法详解"/></a><div class="content"><a class="title" href="/2022/02/09/%E6%96%B9%E6%B3%95%E8%AF%A6%E8%A7%A3/" title="Java方法详解">Java方法详解</a><time datetime="2022-02-09T12:25:00.000Z" title="发表于 2022-02-09 20:25:00">2022-02-09</time></div></div></div></div></div></div></main><footer id="footer" style="background-image: url('https://gitee.com/tanzicai/file-pan/raw/master/img/20220209165412.jpeg')"><div id="footer-wrap"><div class="copyright">&copy;2020 - 2022 By 谭自财</div><div class="framework-info"><span>框架 </span><a target="_blank" rel="noopener" href="https://hexo.io">Hexo</a><span class="footer-separator">|</span><span>主题 </span><a target="_blank" rel="noopener" href="https://github.com/jerryc127/hexo-theme-butterfly">Butterfly</a></div><div class="footer_custom_text">欢迎进入 <a href="https://tanzicai.gitee.io">小谭的部落阁</a>!</div></div></footer></div><div id="rightside"><div id="rightside-config-hide"><button id="readmode" type="button" title="阅读模式"><i class="fas fa-book-open"></i></button><button id="font-plus" type="button" title="放大字体"><i class="fas fa-plus"></i></button><button id="font-minus" type="button" title="缩小字体"><i class="fas fa-minus"></i></button><button id="translateLink" type="button" title="简繁转换">繁</button><button id="darkmode" type="button" title="浅色和深色模式转换"><i class="fas fa-adjust"></i></button><button id="hide-aside-btn" type="button" title="单栏和双栏切换"><i class="fas fa-arrows-alt-h"></i></button></div><div id="rightside-config-show"><button id="rightside_config" type="button" title="设置"><i class="fas fa-cog fa-spin"></i></button><button class="close" id="mobile-toc-button" type="button" title="目录"><i class="fas fa-list-ul"></i></button><button id="go-up" type="button" title="回到顶部"><i class="fas fa-arrow-up"></i></button></div></div><div><script src="/js/utils.js"></script><script src="/js/main.js"></script><script src="/js/tw_cn.js"></script><script src="https://cdn.jsdelivr.net/npm/instant.page/instantpage.min.js" type="module"></script><script>var preloader = {
  endLoading: () => {
    document.body.style.overflow = 'auto';
    document.getElementById('loading-box').classList.add("loaded")
  },
  initLoading: () => {
    document.body.style.overflow = '';
    document.getElementById('loading-box').classList.remove("loaded")

  }
}
window.addEventListener('load',preloader.endLoading())</script><div class="js-pjax"></div><script id="click-heart" src="https://cdn.jsdelivr.net/npm/butterfly-extsrc@1/dist/click-heart.min.js" async="async" mobile="false"></script><script async data-pjax src="//busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script></div></body></html>